package com.lx.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.regex.Pattern;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.lx.dao.ApplicantDao;

@WebServlet("/register.do")
public class RegisterServlet extends HttpServlet {
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
	//	super.doGet(req, resp);
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//super.doPost(req, resp);
		
		PrintWriter out = resp.getWriter();
		HttpSession session  = req.getSession();
		String yzm = (String) session.getAttribute("code1");
		String email = req.getParameter("eamil");
		String password = req.getParameter("password");
		String textch = req.getParameter("textch");
		ApplicantDao ad = new ApplicantDao();
		if(yzm.equals(textch)){
			if(!ad.isExitEmail(email)){
				out.print("<script type='text/javascript'>");
				out.print("alert('邮箱已经存在！');");
				out.print("window.location.href='register.jsp';");
				out.print("document.getElementById('email').focus()");
				out.print("</script>");
			}else{
				ad.saveEmail(email, password);
				req.getRequestDispatcher("registerSucces.jsp").forward(req, resp);
			}
		}else{
			out.print("<script type='text/javascript'>");
			out.print("alert('验证码错误！');");
			out.print("window.location.href='register.jsp';");
			out.print("document.getElementById('email').value="+email);
			out.print("document.getElementById('password').value="+password);
			out.print("document.getElementById('textch').focus()");
			out.print("</script>");
		}
		
	}
	
}
